package org.springboot.szsrcb.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_team_info_log;
import org.springboot.szsrcb.entity.Req_work_log;
import org.springboot.szsrcb.entity.Req_workload_info;
import org.springboot.szsrcb.entity.Test_work_log;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/req")
public class ReqRestController_work_log {

	private static final Logger logger = LoggerFactory.getLogger(ReqRestController_work_log.class);

	@Autowired
	private Req_bill_Service req_bill_Service;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/get_worklog_content", method = { RequestMethod.GET })
	public String get_worklog_content(HttpServletRequest request) {
		String log_id= request.getParameter("log_id");
		Req_work_log r = req_bill_Service.get_a_test_worklog_by_log_id(log_id);
		if(r!=null){			
			String s_tmp=r.getWork_log();			
			return s_tmp;	
		}else{
			return null;	
		}
				
	}
	
	@RequestMapping(value = "/req_modify_detail_work_log_info_onload", method = { RequestMethod.GET })
	public String req_modify_detail_work_log_info_onload(HttpServletRequest request, HttpSession httpSession) {
		String user_name = (String) httpSession.getAttribute("user");
		
		//safas
		String req_id= request.getParameter("req_id");
		List<Req_bill> r = req_bill_Service.req_search(req_id);
		if(r!=null){			
			String s_tmp=trans_2_json_modify_req_work_log_info(req_id,user_name);
			logger.info("req_assign_detail_onload:" + s_tmp);
			return s_tmp;	
		}else{
			return null;	
		}
				
	}


	private String trans_2_json_modify_req_work_log_info(String req_id,String user_name) {
		String select_user=get_select_user_value(req_id);
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");	
		Date date = new java.sql.Date(new java.util.Date().getTime());
		String current_date=sdf.format(date);
		
		String r_start="["
						+ "{type: 'block',  list:["
										+"{type:'block',list:["						
											+"{type : 'hidden',name : 'req_id',label : '需求ID',labelWidth:'70',position:'label-left', value:'"+req_id+"'},"
											+"{type : 'hidden',name : 'is_edit',label : 'is_edit',labelWidth:'70',position:'label-left', value:''}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'select',name : 'user_name',label : '姓名',inputWidth:'150', required:'true',value:'"+user_name+"',options:["
												+"{text: '', value: ''},"
												+select_user
											+ "]},"
											+"{type : 'newcolumn'},"
											+"{type : 'calendar',name : 'work_date',label : '填报日期',position:'label-left',offsetLeft:'50', value:'"+current_date+"',required:'true'}"
										+"]},"
										+"{type:'block',list:["
											+"{type : 'editor',name : 'work_log',label : '工作日志',position:'label-top', required:'true',inputWidth:'750', inputHeight:'200'}"																					
										+"]},"											
										+"{type:'block',list:["
											+"{type : 'button',name : 'save_btn',value : '保存信息'}"			
										/*	+"{type : 'newcolumn'},"
											+"{type : 'button',name : 'modify_btn',value : '修改信息'},"
											+"{type : 'newcolumn'},"
											+"{type : 'button',name : 'del_btn',value : '删除信息'}"*/
										+"]}"										
																				
									+"]"
							+ "}"
						+"]";
		String r_s=r_start;
		logger.info("retrun info :" + r_start);
		return r_s;
	}
	
	
	
	
	private String get_select_user_value(String req_id) {
		Req_base_info req_base_info=req_bill_Service.search_req_base_info(req_id);
		
		//req_head
		String req_head=req_base_info.getReq_head();
		//req_analyst
		String tmp=req_base_info.getReq_analyst();
		String[] req_analyst=tmp.split(",");
		
		//req_supervisor
		String tmp2=req_base_info.getReq_supervisor();
		String[] req_supervisor=tmp2.split(",");
		String r="";
		List<String> user = new ArrayList<String>(); 
		user.add(req_head);
		//user[0]=;
		for (int i = 0; i < req_analyst.length; i++) {
			String string = req_analyst[i];
			boolean compare_r=false;
			for (int j = 0; j < user.size(); j++) {
				if(string.equals(user.get(j))){
					compare_r=true;
				}
			}			
			if(compare_r==false){
				user.add(string);
			}
		}
		
		for (int i = 0; i < req_supervisor.length; i++) {
			String string = req_supervisor[i];
			boolean compare_r=false;
			for (int j = 0; j < user.size(); j++) {
				if(string.equals(user.get(j))){
					compare_r=true;
				}
			}			
			if(compare_r==false){
				user.add(string);
			}
		}	
		
		for (int i = 0; i < user.size(); i++) {
			String string =user.get(i);
			String t="";
			if (i==user.size()) {
				 t="{text: '"+string+"', value: '"+string+"'}";
			} else {
				 t="{text: '"+string+"', value: '"+string+"'},";
			}			
			r=r+t;
		}	
		
			
			
		/*r="{text: '"+req_head+"', value: '"+req_head+"'},";
		for (int i = 0; i < req_analyst.length; i++) {
			String string = req_analyst[i];
			String t="{text: '"+string+"', value: '"+string+"'},";
			r=r+t;
		}
		for (int i = 0; i < req_supervisor.length; i++) {
			String string = req_supervisor[i];
			String t="";
			if (i==req_supervisor.length) {
				 t="{text: '"+string+"', value: '"+string+"'}";
			} else {
				 t="{text: '"+string+"', value: '"+string+"'},";
			}			
			r=r+t;
		}	*/
		
		return r;
	}


	@RequestMapping(value = "/req_modify_detail_work_log_info_grid", method = { RequestMethod.GET })
	public String req_modify_detail_workload_info_grid(HttpServletRequest request,HttpSession httpSession) {
		logger.info("access /req/req_modify_detail_work_log_info_grid");		
		String req_id= request.getParameter("req_id");
		logger.info("req_id ="+req_id);
		String user=(String) httpSession.getAttribute("user"); 
		//if(r!=null){			
			String s_tmp=trans_2_json_req_modify_detail_worklog_info_grid(req_id,user);
			logger.info("req_modify_detail_team_info_grid :" + s_tmp);
			return s_tmp;	
		/*}else{
			return null;	
		}*/
	}


	private String trans_2_json_req_modify_detail_worklog_info_grid(String req_id,String user_name) {
				String r_s="";				
				String r_start="{rows:[";		
				String r_end="]}";
				boolean is_common_user=true;
				Req_base_info req_base_info=req_bill_Service.search_req_base_info(req_id);
				String get_req_head =req_base_info.getReq_head();
				//String get_req_anal =req_base_info.getReq_analyst();
				
				if(user_name.equals(get_req_head)){
					is_common_user=false;
				}
				
				boolean check_u=userService.is_leader(user_name);
				if(check_u){
					is_common_user=false;
				}
				
				List<Req_work_log> log_list=null;
				if(is_common_user){
					 log_list = req_bill_Service.search_req_work_log_by_name(req_id,user_name);
				}else{
					 log_list = req_bill_Service.search_req_work_log(req_id);
				}
				
				if (log_list==null) {
					return null;
				} else {
					for (int i = 0; i < log_list.size(); i++) {
						String tmp_start="{ id:"+(i+1)+",locked:false,data:[";
						String tmp_end="]}";
						String tmp_content="";
						Req_work_log item=log_list.get(i);
						//tmp_content="'0',";
						tmp_content=tmp_content+"'"+item.getId()+"',";
						tmp_content=tmp_content+"'"+item.getUser_name()+"',";
						SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
						String date_tmp="";
						if(item.getWork_date()!=null){
							 date_tmp=sdf.format(item.getWork_date());
						}
						
						tmp_content=tmp_content+"'"+date_tmp+"',";
						if(i==log_list.size()){
							String content="";
							content=item.getWork_log();
							content = content.replaceAll("</?[^<]+>", "");
							//content=content.substring(0, 30)+"......";
							if(content.length()>50){
								content=content.substring(0, 30)+"......";	
							}
							tmp_content=tmp_content+"'"+content+"'";
							//tmp_content=tmp_content+"'编辑^javascript:edit("+(i+1)+");^_top',";
							//tmp_content=tmp_content+"'删除^javascript:del("+(i+1)+");^_top'";
							tmp_content=tmp_content+"'',";
							tmp_content=tmp_content+"''";
							tmp_end="]}";
						}else{
							String content="";
							content=item.getWork_log();
							content = content.replaceAll("</?[^<]+>", "");
							//content=content.substring(0, 30)+"......";
							if(content.length()>50){
								content=content.substring(0, 30)+"......";	
							}
							tmp_content=tmp_content+"'"+content+"',";
							//tmp_content=tmp_content+"'编辑^javascript:edit("+(i+1)+");^_top',";
							//tmp_content=tmp_content+"'删除^javascript:del("+(i+1)+");^_top',";
							tmp_content=tmp_content+"'',";
							tmp_content=tmp_content+"'',";
							tmp_end="]},";
						}
						
						
						r_s=r_s+tmp_start+tmp_content+tmp_end;
					}
					
				}		
				r_s=r_start+r_s+r_end;
				return r_s;
	}	
	
	private Map<String, Object> trans_json_value(Map<String, Object> map){
		String return_v="";
		String tmp_content="";
		
		int count_value=(int)map.get("count_value");
		String req_id=(String)map.get("req_id");
		String input_value=(String)map.get("input_value");
		//Req_base_info req_base_info=(Req_base_info)map.get("req_base_info");
		String role=(String)map.get("role");
		if (input_value==null||input_value.equals("")) {
			tmp_content="";
		} else {
			String[] s_array=input_value.split(",");
			for (int i = 0; i < s_array.length; i++) {				
					String tmp_start="{ id:"+(count_value)+",locked:false,data:[";
					String tmp_end="]}";
					if((i+1)<s_array.length){
						tmp_end=tmp_end+",";
					}
					count_value=count_value+1;
					//ID
					tmp_content="'"+count_value+"',";  
					//String tmp_content="";
					//name
					tmp_content=tmp_content+"'"+s_array[i]+"',";
					//角色
					tmp_content=tmp_content+"'"+role+"',";
					//计划完成日	
					Req_workload_info obj=req_bill_Service.search_a_req_workload_info(req_id,s_array[i]);
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					if(obj==null){
						//计划完成日	
						tmp_content=tmp_content+"'',";
						//实际完成日
						tmp_content=tmp_content+"'',";	
						//计划工作量
						tmp_content=tmp_content+"'',";	
						//实际工作量
						tmp_content=tmp_content+"'',";							
					}else{
						
						String date_tmp= sdf.format(obj.getPlan_complete_date());	
						//计划完成日	
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//实际完成日
						date_tmp= sdf.format(obj.getActual_complete_date());			
						tmp_content=tmp_content+"'"+date_tmp+"',";
						//计划工作量
						tmp_content=tmp_content+"'"+obj.getPlan_workload()+"',";
						//实际工作量
						tmp_content=tmp_content+"'"+obj.getActual_workload()+"',";
					}												
					tmp_content=tmp_start+tmp_content+tmp_end;		
					return_v=return_v+tmp_content;
			}
		}
		//return_v=tmp_content;
		Map<String, Object> map_r = new HashMap<String, Object>();
		map_r.put("json_value", return_v);
		map_r.put("count_value", count_value);				
		return map_r;
		
	}
	
	@RequestMapping(value = "/req_modify_detail_work_log_info_save")
	public boolean req_modify_detail_work_log_info_save(HttpServletRequest request, Model model,Req_work_log req_work_log) {
		logger.info("access /req/req_modify_detail_work_log_info_save");		
		model.addAttribute("Req_work_log", req_work_log);
		
		logger.info("req_id ="+req_work_log.getReq_id());
		boolean r=req_bill_Service.save_req_work_log(req_work_log);
		
		model.addAttribute("req_id", req_work_log.getReq_id());
		// model.addAttribute("name", name);
		return r;
		/*}else{
			return null;	
		}*/
	}
	
	
	@RequestMapping(value = "/req_modify_detail_work_log_info_grid_delete")
	public String req_modify_detail_work_log_info_grid_delete(HttpServletRequest request,HttpSession httpSession) {
		logger.info("access /req/req_modify_detail_work_log_info_grid_delete");		
		String id= request.getParameter("id");
		req_bill_Service.delet_a_work_log(id);
		String req_id= request.getParameter("req_id");
		String s_tmp="";
		//String s_tmp=trans_2_json_req_modify_detail_worklog_info_grid(req_id);
		String user_name=(String) httpSession.getAttribute("user"); 
		s_tmp=trans_2_json_req_modify_detail_worklog_info_grid(req_id,user_name);
		
		return s_tmp;
		/*}else{
			return null;	
		}*/
	}
	

}
